<?php
/**
 * 用户控制器
 * Created by  你老板
 * User: 你老板
 * Date: 2018/11/22
 * Time: 上午 11:10
 */
namespace app\home\controller;
use think\Controller;
use \think\Db;
class Product extends Controller {
		public function productview(){
			  $db = Db::name('product');
				$data['id'] = $_POST['id'];


				$list = $db->where($data)->find();
				$pic = json_decode($list['pic'],true);
				$guidance_price = round($list['guidance_price']/10000,2);
				// 对比

				$contrast = json_decode($list['contrast'],true);
				$contrastjson = $contrast['duibi'];
				$jbc_title = json_decode($contrastjson['jbc_title'],true);
				$jbc_titles = json_decode($contrastjson['jbc_titles'],true);
				$qita_title = json_decode($contrastjson['qita_title'],true);
				// 对比结束
				$arr['id'] = $list['id'];
				$arr['charge'] = $list['charge'];//手续费
				$arr['name'] = $list['name'];
				$arr['package_price'] = $list['package_price'];//包牌价
				$arr['level'] = $list['level'];//包牌价
				$arr['vehicle_price'] = $list['vehicle_price'];//汽车总价
				$arr['title'] = $list['title'];//车辆名称
				$arr['volume'] = $list['volume'];//已售/销量
				$arr['pic'] = $pic;//图片
				$arr['contrast'] = ['jbc_title'=>$jbc_title,'jbc_titles'=>$jbc_titles,'qita_title'=>$qita_title];//骏宝驰对比json
				$arr['guidance_price'] = $guidance_price;//车辆名称
				//配置
				$arr['configure'] = json_decode($list['configure'],true);
				//必要花费获取
				$db = $this->necessary_cost($list['id']);
				$huafei = 0;
				foreach ($db as $key => $value) {
					$huafei +=$value['necessary_charges_title'];
				}
				$arr['necessary_charges_title'] = $huafei;




				//第三者保险获取
				$dbs= $this->insurance($data['id']);
				$money = $this->all_insurances($dbs);//一维数组相加
				// $insurance = $dbs[0]['commercial_insurance_title'];
				// $lic = $this->insurance_sele1($insurance);
				// $licd = $this->disanzhes($lic);//第三者保险金额

				// 玻璃单独破碎险(元)
				// $insurance1 = $dbs[3]['commercial_insurance_title'];
				// $li1 = $this->insurance_sele1($insurance1);
				// $licds = $this->disanzhes($li1);//第三者保险金额

				// 玻璃单独破碎险(元)
				// $insurance2 = $dbs[8]['commercial_insurance_title'];
				// $li2 = $this->insurance_sele1($insurance2);
				// $licd2 = $this->disanzhes($li2);//第三者保险金额

				$arr['money'] = $money;
				$dow_payments = json_decode($list['dow_payments'],true);
				$arr['shoufusd'] = $dow_payments[0]['dow_payments_title'];

				//还款年限
				$dow_payments1 = json_decode($list['repayments'],true);
				$arr['repayments'] = $dow_payments1[0]['repayments_title'];
				// dump($arr);
					// dump($licd2['2千']);
					// dump($licds['国产']);
					// dump($licd['5万']);
					//
					// die;
				echo json_encode($arr);
		}
		//必要花费获取
		public function necessary(){
				$id = $_POST['id'];
				$db = $this->necessary_cost($id);
				echo json_encode($db);
		}
		//必要花费
		public function necessary_cost($id){
			$db = Db::name('product');
			$data['id'] = $id;
			$list = $db->where($data)->field('necessary_charges')->find();
			$contrast = json_decode($list['necessary_charges'],true);
			return $contrast;
		}
		//商业保险获取
		public function insurance_sele(){
			$id = 11;
			$db = [];
			$db = $this->insurance($id);
			// 第三者责任险(元)
			$insurance = $db[0]['commercial_insurance_title'];
			$li = $this->insurance_sele1($insurance);
			$db['dishanzhelist'] = $li;
			$db['disanz'] = $li[0]['yuan'];
			// 玻璃单独破碎险(元)
			$insurance1 = $db[3]['commercial_insurance_title'];
			$li1 = $this->insurance_sele1($insurance1);
			$db['insurance'] = $li1;
			$db['guoji'] = $li1[0]['yuan'];
			// 车身刮痕险(元)
			$insurance2 = $db[8]['commercial_insurance_title'];
			$li2 = $this->insurance_sele1($insurance2);
			$db['huahens'] = $li2;
			$db['huahen'] = $li2[0]['yuan'];
			echo json_encode($db);

		}
		//获取第三者
		public function insurance_sele1($arr){
			$baoxian1 = explode('|',$arr);
			foreach ($baoxian1 as $key => $value) {
					$baoxian1 = explode(',',$value);
					$arrs[] = [
						'jiaqian' =>$baoxian1[0],
						'yuan' =>$baoxian1[1],
					];
			}
			return $arrs;
		}
		//商业保险
		public function insurance($id){
			$db = Db::name('product');
			$data['id'] = $id;
			$list = $db->where($data)->field('commercial_insurance')->find();
			$contrast = json_decode($list['commercial_insurance'],true);

			return $contrast;
		}
		//统计商业保险总额
		public function all_insurances($arr){
			$money = 0;
			$money = $arr[1]['commercial_insurance_title']
						+ $arr[2]['commercial_insurance_title']
						+ $arr[4]['commercial_insurance_title']
						+ $arr[5]['commercial_insurance_title']
						+ $arr[6]['commercial_insurance_title']
						+ $arr[7]['commercial_insurance_title']
						+ $arr[9]['commercial_insurance_title']
			;
			return $money;
		}
		//商业保险-第三者
		public function disanzhes($arr){
				$name = [];
				foreach ($arr as $key => $value) {
						$name[$value['jiaqian']] = $value['yuan'];
				}
					return $name;
		}
		//首付比例
		public function payments(){
			$db = Db::name('product');
			$data['id'] = $_POST['id'];
			$list = $db->where($data)->field('dow_payments')->find();
			echo $list['dow_payments'];

		}
		//还款年限
		public function period(){
			$db = Db::name('product');
			$data['id'] = $_POST['id'];
			$list = $db->where($data)->field('repayments')->find();
			echo $list['repayments'];
		}
		//全款统计
		public function quankuan(){
			$db = Db::name('product');
			$data['id'] = $_POST['id'];//车辆id
			$info = $_POST['necessary_charges'];//必要花费
			$sheny = $_POST['repayments'];//商业保险
			$list = $db->where($data)->find();

			// dump($list);
			//判断有没有填写优惠价格  如果有就按照vehicle_price去算
			if (isset($list['vehicle_price'])) {
					$money = $list['vehicle_price'];//优惠价
			}else{
				$money = $list['package_price'];//指导价
			}

			$moneys = round($money+$sheny+$info,2);
			echo json_encode($moneys);

		}
		// 		算法：
		//
		// 月供=[(指导价+必须花费上牌保险等)-（首付比例*指导价）]*年份利息/还款期数
		//
		// 首付=指导价*首付比例+必要花费+商业保险
		//
		// 必要花费=购置税+上牌费+车船税+交强险

		//贷款方案算法
		public function daikuan(){
			$db = Db::name('product');
			$data['id'] = 11;//车辆id
			$list = $db->where($data)->find();
			if (isset($list['vehicle_price'])) {
					$money = $list['vehicle_price'];//优惠价
			}else{
				$money = $list['package_price'];//指导价
			}
			$nianx = json_decode($list['repayments'],true);
			$nianfens = $this->nianxians($nianx);
			$shoufu = $_POST['shoufu'];//首付比例
			$nianxian = $_POST['nianxian_n'];//还款年限
			$nianxianlisi = $nianfens[$_POST['nianxian_n']];//年份利息
			$biyaohuafei = $_POST['necessary_charges_title'];//必要花费
			$shangyebianx = $_POST['shany'];//商业保险
			$quanbaojia = round($money+$biyaohuafei+$shangyebianx,2);//全包价
			// 月供
			$i = $money+$biyaohuafei+$shangyebianx;//指导价+必须花费上牌保险等
			$b = $shoufu*$money/100;//首付比例*指导价
			$a =$i-$b;//[(指导价+必须花费上牌保险等)-（首付比例*指导价）]
			$l = $a*$nianxianlisi+$a;
			$s = $nianxian*12;
			$r = round($l/$s,2);//年份利息/还款期数
			// 首付
			$u =  round($b+$biyaohuafei+$shangyebianx,2);//购置税+上牌费+车船税+交强险
			//总价
			$z = round($u+($r*$s));//贷款总价
			//多花费
			$o = round($z-$quanbaojia,2);



			$arr['yuegong'] = $r;//月供
			$arr['shoufu'] = $u;//首付
			$arr['duohuafei'] = $o;//多花费
			$arr['zongjia'] = $z;//总价
			echo json_encode($arr);
			// var_dump($arr);


		}
		//还款年限
		public function nianxians($arr){
			foreach ($arr as $key => $value) {
					$name[$value['repayments_title']] = $value['repayments_remarks'];
			}

			return $name;

		}
		//添加浏览历史
		public function browsing_history(){
			$db = Db::name('browsing_history');
			$data['member_id'] = $_POST['user_id'];
			$data['product_id'] = $_POST['id'];
			$data['addtime'] = time();
			$info = $db->where(['member_id'=>$data['member_id'],'product_id'=>$data['product_id']])->find();
			if (!$info) {
				$db->insert($data);
			}
		}
		//关注
		public function follow_product(){
			$db = Db::name('follow_product');
			$data['member_id'] = $_POST['user_id'];
			$data['product_id'] = $_POST['id'];
			$data['addtime'] = time();
			$info = $db->where(['member_id'=>$data['member_id'],'product_id'=>$data['product_id']])->find();
			if (!$info) {
				$db->insert($data);

			}



		}































}
